/*
 * Implementa carrossel de objetos e envia para o canal
 */
package ProviderStreamWorker;

import ProviderStreamWorker.Utils.CircularArrayList;
import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;

/**
 *
 * @author Fabrício Alves
 * @author Antônio Pedro
 */
public class StreamObjectsCarousel extends Thread {

    private DatagramSocket socket;
    private BufferedReader in;
    private String address;
    private int port;

    public StreamObjectsCarousel(String address, int port) throws IOException {
        this("ObjectsStreamThread", address, port);
    }

    public StreamObjectsCarousel(String name, String address, int port) throws IOException {
        super(name);
        this.address = address;
        this.port = port;
        socket = new DatagramSocket(port);
    }

    public ArrayList<byte[]> prepareCarouselFiles() {
        //TODO: pegar todos os arquivos do diretorio
        File diretorio = new File("objects");
        File[] files = diretorio.listFiles();
        ArrayList<byte[]> chunks = new ArrayList<byte[]>();
        try {
            for (int f = 0; f < files.length; f++) {
                if (!files[f].isDirectory()) {
                    StreamObject streamObject = new StreamObject(files[f], 992);
                    chunks.addAll(streamObject.getChunks());
                }
            }
        } catch (NullPointerException e) {
            System.err.println("There aren't objects to stream.");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return chunks;
    }

    private void startCarousel(ArrayList<byte[]> chunks) throws UnknownHostException, IOException {
        InetAddress group = InetAddress.getByName(this.address);
        while (true) {
            for (byte[] pack : chunks) {
                DatagramPacket packet = new DatagramPacket(pack, pack.length, group, this.port);
                socket.send(packet);
                try {
                    System.out.println("Pacote enviado.");
                    sleep((long) (Math.random() * 1000));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void run() {
        try {
            //runTest();
            ArrayList<byte[]> carouselFiles = this.prepareCarouselFiles();
            startCarousel(carouselFiles);
        } catch (Exception ex) {
            Logger.getLogger(StreamObjectsCarousel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void runTest() {
        for (int i = 0; true; i++) {
            try {
                byte[] buf = new byte[256];

                String message = "This is the message: " + i;

                buf = message.getBytes();

                InetAddress group = InetAddress.getByName(this.address);
                DatagramPacket packet = new DatagramPacket(buf, buf.length, group, this.port);
                socket.send(packet);
                try {
                    System.out.println("Descansando...");
                    sleep((long) (Math.random() * 5000));
                    System.out.println("Voltando ao trabalho");
                } catch (InterruptedException ex) {
                    Logger.getLogger(StreamObjectsCarousel.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
